//
//  FirstViewPullToGetMoreDataCell.m
//  WEIMO
//
//  Created by Lixiaoyong on 13-7-2.
//  Copyright (c) 2013年 广州从兴电子开发有限公司. All rights reserved.
//

#import "FirstViewPullToGetMoreDataCell.h"

#define kNormalTitle (@"点击获取更多")
#define kDownloadTitle (@"正在获取数据")

@implementation FirstViewPullToGetMoreDataCell
@synthesize getMoreDataButton;
@synthesize indicatorView;
@synthesize delegate;

- (void)dealloc
{
    self.getMoreDataButton = nil;
    self.indicatorView = nil;
    self.delegate = nil;
    [super dealloc];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        CGRect rect = CGRectZero;
        self.getMoreDataButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.getMoreDataButton.frame = rect;
        [self.getMoreDataButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.getMoreDataButton addTarget:self action:@selector(clickedGetMoreDataButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.getMoreDataButton];
        
        self.indicatorView = [[[UIActivityIndicatorView alloc] initWithFrame:rect] autorelease];
        [self addSubview:self.indicatorView];
        self.indicatorView.hidden = YES;
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

- (void)drawRect:(CGRect)rect
{
    float oX = (rect.size.width - 120) / 2.0;
    float oY = 5.0;
    CGRect subR = CGRectMake(oX, oY, 120, 44);
    self.getMoreDataButton.frame = subR;
    
    oX = oX - 44;
    subR = CGRectMake(oX, oY, 44, 44);
    self.indicatorView.frame = subR;
}

#pragma mark - event

- (void)clickedGetMoreDataButton
{
    [self.getMoreDataButton setTitle:kDownloadTitle forState:UIControlStateNormal];
    
    if (self.delegate)
    {
        [self.delegate clickedButtonToGetMoreData:self];
        
        self.indicatorView.hidden = NO;
        [self.indicatorView startAnimating];
    }
}

#pragma mark - protected methods

//恢复初始状态
- (void)restoreInitialState
{
    [self.getMoreDataButton setTitle:kNormalTitle forState:UIControlStateNormal];
    self.getMoreDataButton.hidden = NO;
}

//刷新数据
//flag:YES--有数据   NO--没有数据了
- (void)stopAnimationOfIndicatorView:(BOOL)flag
{
    [self.indicatorView stopAnimating];
    self.indicatorView.hidden = YES;
    
    if (flag)
    {
        [self.getMoreDataButton setTitle:kNormalTitle forState:UIControlStateNormal];
        self.getMoreDataButton.hidden = NO;
    }
    else
    {
        [self.getMoreDataButton setTitle:@"" forState:UIControlStateNormal];
        self.getMoreDataButton.hidden = YES;
    }
}

@end